package cn.yqx.bankshop.oauth2.handle;

import cn.yqx.bankshop.oauth2.authentication.code.ValidateImageCodeException;
import cn.yqx.bankshop.oauth2.authentication.code.ValidateImageCodeExpireException;
import cn.yqx.bankshop.oauth2.authentication.mobile.ValidateCodeException;
import cn.yqx.bankshop.oauth2.authentication.mobile.ValidateCodeExpireException;
import cn.yqx.bankshop.result.Result;
import cn.yqx.bankshop.result.ResultCode;
import com.alibaba.fastjson.JSON;
import org.springframework.security.authentication.*;
import org.springframework.security.core.AuthenticationException;
import org.springframework.security.web.authentication.AuthenticationFailureHandler;
import org.springframework.stereotype.Component;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;

/**
 * @author  Lu
 * @desc 自定义form登录失败处理器
 */

@Component
public class CustomizeAuthenticationFailureHandler implements AuthenticationFailureHandler {

    @Override
    public void onAuthenticationFailure(HttpServletRequest httpServletRequest, HttpServletResponse httpServletResponse, AuthenticationException e) throws IOException, ServletException {
        //返回json数据
        Result result = null;
        if (e instanceof AccountExpiredException) {
            //账号过期
            result = result.failed(ResultCode.TOKEN_INVALID_OR_EXPIRED);
        } else if (e instanceof BadCredentialsException) {
            //密码错误
            result = result.failed(ResultCode.USERNAME_OR_PASSWORD_ERROR);
        } else if (e instanceof CredentialsExpiredException) {
            //密码过期
            result = result.failed(ResultCode.USER_CREDENTIALS_EXPIRED);
        } else if (e instanceof DisabledException) {
            //账号不可用
            result = result.failed(ResultCode.USER_ACCOUNT_DISABLE);
        } else if (e instanceof LockedException) {
            //账号锁定
            result = result.failed(ResultCode.USER_ACCOUNT_LOCKED);
        } else if (e instanceof InternalAuthenticationServiceException) {
            //用户不存在
            result = result.failed(ResultCode.USER_NOT_EXIST);
        } else if(e instanceof ValidateCodeException){
            result = result.failed(ResultCode.VALIDATE_CODE_ERROR);
        } else if(e instanceof ValidateCodeExpireException){
            result = result.failed(ResultCode.VALIDATE_CODE_EXPIRE_EXCEPTION);
            result.setMsg(e.getMessage());
        } else if(e instanceof ValidateImageCodeExpireException){
            result = result.failed(ResultCode.VALIDATE_CODE_EXPIRE_EXCEPTION);
            result.setMsg(e.getMessage());
        } else if(e instanceof ValidateImageCodeException){
            result = result.failed(ResultCode.VALIDATE_CODE_ERROR);
            result.setMsg(e.getMessage());
        }
        else{
            //其他错误
            result = result.failed(ResultCode.COMMON_FAIL);
        }
        httpServletResponse.setContentType("text/json;charset=utf-8");
        httpServletResponse.getWriter().write(JSON.toJSONString(result));
    }
}
